#!/usr/bin/env node

// https://cirosantilli.com/cirodown#build-sass

const fs = require('fs');
const path = require('path');

const sass = require('sass');

const cirodown_nodejs = require('cirodown/nodejs');

const katex_fonts_src_dir = path.join(cirodown_nodejs.PACKAGE_NODE_MODULES_PATH, 'katex', 'dist', 'fonts')
const fonts_dist_dir = path.join(cirodown_nodejs.DIST_PATH, 'fonts')
fs.mkdirSync(cirodown_nodejs.DIST_PATH, {recursive: true})
fs.mkdirSync(fonts_dist_dir, {recursive: true})

const dir = fs.opendirSync(katex_fonts_src_dir)
let dirent
while ((dirent = dir.readSync()) !== null) {
  fs.copyFileSync(
    path.join(katex_fonts_src_dir, dirent.name),
    path.join(fonts_dist_dir, dirent.name)
  )
}
dir.closeSync()

fs.mkdirSync(cirodown_nodejs.DIST_PATH, {recursive: true})
const scss_input = fs.readFileSync(path.join(
  __dirname, cirodown_nodejs.PACKAGE_SASS_BASENAME)).toString();
let result;
result = sass.renderSync({
  data: scss_input,
  outputStyle: 'compressed',
  includePaths: [cirodown_nodejs.PACKAGE_NODE_MODULES_PATH],
});
fs.writeFileSync(cirodown_nodejs.PACKAGE_OUT_CSS_EMBED_PATH, result.css);
